using System;
using System.Collections;
using System.Collections.Generic;
using HR60_PanelSoft.FileUtils;
using HR60_PanelSoft.Model.BaseModel;

namespace HR60_PanelSoft.Model
{
	/// <summary>
	/// 面板创建器
	/// </summary>
	public class PanelCreator
	{
		/// <summary>
		/// 面板类型全集
		/// </summary>
		public List<PanelType> panelTypes = null;

		/// <summary>
		/// 从文件载入类型全集
		/// </summary>
		public void loadTypes()
		{
			panelTypes = JsonFileUtils.loadPanelTypes ();
		}

		/// <summary>
		/// 获得面板类型全集
		/// </summary>
		/// <returns>The types.</returns>
		public List<PanelType> getTypes()
		{
			return panelTypes;
		}

		/// <summary>
		/// 根据类型创建设备
		/// </summary>
		/// <param name="type">Type.</param>
		public Panel create(PanelType type)
		{
			Panel panel = new Panel ();
			panel.type = type;
			panel.name = type.name;

			// 初始化负载线路
			for(int i=0; i<type.lPinLen; i++)
			{
				LoadPin pin = new LoadPin ();
				pin.displayName = "L" + (i + 1);
				pin.type = PinType.L;
				pin.index = i;
				panel.loadPins.Add (pin);
			}
			for(int i=0; i<type.tPinLen; i++)
			{
				LoadPin pin = new LoadPin ();
				pin.displayName = "T" + (i + 1);
				pin.type = PinType.T;
				pin.index = type.lPinLen + i;
				panel.loadPins.Add (pin);
			}
			if (type.hasM485Model)
			{
				panel.m485Pin = new M485Pin ();
			}

			// 初始化按键页
			for (int i=0; i<type.maxPage; i++)
			{
				PanelPage page = new PanelPage ();
				for (int j=0; j<type.buttonsPerPage; j++) 
				{
					PanelButton btn = new PanelButton ();
					page.buttons.Add (btn);
				}
				panel.pages.Add (page);
			}

			// 初始化场景页
			for (int i=0; i<type.maxScenePage; i++)
			{
				PanelPage page = new PanelPage ();
				for (int j=0; j<type.buttonsPerPage; j++) 
				{
					PanelButton btn = new PanelButton ();
					page.buttons.Add (btn);
				}
				panel.scenePages.Add (page);
			}

			// 初始化高级控制页
			if (type.hasExPage)
			{
				panel.airconPage = new ExPanelPage ();
				panel.newWindPage = new ExPanelPage ();
				panel.heatingPage = new ExPanelPage ();
				panel.bgmPage = new ExPanelPage ();
			}

			// 外部信号
			if (type.hasSignalModel)
			{
				panel.signal = new Signal();
			}

			return panel;
		}
	}
}

